package com.maoye.ossp.biz.statement.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.hotent.base.api.query.QueryFilter;
import com.hotent.base.api.query.QueryOP;
import com.maoye.ossp.biz.brand.entity.Supplier;
import com.maoye.ossp.biz.brand.service.impl.SupplierServiceImpl;
import com.maoye.ossp.biz.example.entity.Student;
import com.maoye.ossp.biz.statement.dao.IBizStatementPayinDetailDao;
import com.maoye.ossp.biz.statement.entity.BizStatementPayinDetail;
import com.maoye.ossp.biz.statement.entity.BizStatementPayinSlip;
import com.maoye.ossp.biz.statement.entity.StatementMainConsortium;
import com.maoye.ossp.biz.statement.service.IBizStatementPayinDetailService;
import com.maoye.ossp.biz.statement.service.IBizStatementPayinSlipService;
import com.maoye.ossp.common.entity.Page;
import com.maoye.ossp.common.web.BaseController;
import com.maoye.ossp.sys.org.util.UserUtils;

/**
 * 缴款通知单 Controller
 * 
 * @author yangyf
 * 
 *         2014年3月6日
 */
@Controller
@RequestMapping(value = "/biz/statement/stm/payinslip/")
public class BizStatementPayinSlipController extends BaseController {
	@Resource
	private IBizStatementPayinSlipService statementPayinSlipService;

	@Resource
	private IBizStatementPayinDetailService payinDetailService;
	
	@Resource
	private SupplierServiceImpl supplierServiceImpl;
	
	@RequestMapping(value = "list")
	public String list(BizStatementPayinSlip statementPayinSlip,
			HttpServletRequest request, Model model) {
		// 获取登陆用户Id
		Long userId = UserUtils.getCurrentUserId();
		// 根据当前用户Id 如果是供应商 获取用户获取供应商信息
		Supplier supplier = supplierServiceImpl.get(userId);
		QueryFilter queryFilter = getQuerFilter(request, statementPayinSlip);
		// 设置供应商编码
		if (supplier != null) {
			queryFilter.addFilter("supplierNo", supplier.getSupplierNo(),
					QueryOP.EQUAL);
		}
		Page<BizStatementPayinSlip> page = (Page<BizStatementPayinSlip>) statementPayinSlipService
				.find(queryFilter);
		model.addAttribute("page", page);
		model.addAttribute("statementPayinSlip", statementPayinSlip);
		return resultPage("/biz/statement/payinslip/list", model, request);
	}

	/**
	 * 查询详细信息
	 * 
	 * @param id
	 *            参数
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "{id}/{billNo}/getByPayinId")
	public String getByPayinId(@PathVariable String id,@PathVariable Long billNo,
			HttpServletRequest request, Model model) {
		BizStatementPayinSlip payinslip = statementPayinSlipService
				.getPayinId(id);
		BizStatementPayinDetail detail = new BizStatementPayinDetail();
		QueryFilter queryFilter = getQuerFilter(request, detail);
		queryFilter.getParams().put("payinSlipId", billNo);
		Page<BizStatementPayinDetail> page = (Page<BizStatementPayinDetail>) payinDetailService
				.find(queryFilter);
		model.addAttribute("payinslip", payinslip);
		model.addAttribute("page", page);
		model.addAttribute("billNo", billNo);
		return "/biz/statement/payinslip/view";
	}
	
	/**
	 * 查询详细信息
	 * 
	 * @param id
	 *            参数
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "print")
	public String print(
			HttpServletRequest request, Model model) {
		String id=request.getParameter("id");
		String billNo=request.getParameter("billNo");
		BizStatementPayinSlip payinslip = statementPayinSlipService
				.getPayinId(id);
		BizStatementPayinDetail detail = new BizStatementPayinDetail();
		QueryFilter queryFilter = getQuerFilter(request, detail);
		queryFilter.getParams().put("payinSlipId", billNo);
		Page<BizStatementPayinDetail> page = (Page<BizStatementPayinDetail>) payinDetailService
				.find(queryFilter);
		model.addAttribute("payinslip", payinslip);
		model.addAttribute("page", page);
		model.addAttribute("billNo", billNo);
		return "/biz/statement/payinslip/print";
	}

	

	/**
	 * 查看缴款明细
	 * 
	 * @param billNo
	 *            参数
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "{billNo}/getByPayinPid")
	public String getByPayinPid(@PathVariable Long billNo,
			HttpServletRequest request, Model model) {
		BizStatementPayinDetail detail = new BizStatementPayinDetail();
		QueryFilter queryFilter = getQuerFilter(request, detail);
		queryFilter.getParams().put("payinSlipId", billNo);
		Page<BizStatementPayinDetail> page = (Page<BizStatementPayinDetail>) payinDetailService
				.find(queryFilter);
		model.addAttribute("page", page);
		model.addAttribute("billNo", billNo);
		return "/biz/statement/payinslip/detailview";
	}

	/**
	 * 条件查询
	 * 
	 * @param statementPayinSlip
	 *            参数
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "getByPayinSlip")
	public String getByPayinSlip(BizStatementPayinSlip statementPayinSlip,
			Model model) {
		Map map = new HashMap();
		map.put("createTime", statementPayinSlip.getCreateTime());
		map.put("supplierNo", statementPayinSlip.getSupplierNo());
		List<BizStatementPayinSlip> list = statementPayinSlipService
				.getByPayin(map);
		model.addAttribute("list", list);
		return "/biz/statement/payinslip/list";
	}
}
